/**
 * Torchd UI Menu Bar
 *
 * Initializes and displays a menu bar similar to those in desktop operating systems
 *
 * @package Torchd Framework
 * @subpackage UI
 * @author Jason Raede <jason@torchdm.com>
 */
torchd.ui.menuBar = function(menus) {
	this.init(menus);
};

/**
 * Initialize the menu with a menu definition array.
 *
 * Menu definitions are structured as follows:
 *			{
 *				menu_name (e.g. File): [
 *					{
 *						name:'button_name' (e.g. "new"),
 *						display:'button_display' (e.g. "New"),
 *						defaultState:'enabled' or 'disabled',
 *						buttons: Optional array of same structure of this
 *									for multi-level menus
 *						clickAction: function when button is clicked
 *					},
 *					{
 *						// Another button
 *					}
 *				],
 *				another_menu (e.g. Edit): [
 *				
 *				],
 *			}
 */
torchd.ui.menuBar.prototype.init = function(menus) {
	var self = this;
	var menuElement = torchd.ui.createElement('ul').addClass('torchd-ui-menu').prependTo($('body'));
	this.menuElement = menuElement;
	for(var index in menus) {
		var menuInfo = menus[index];
		var button = $('<li/>').addClass('torchd-ui-menu-tab').html(index).click(function(e) {
			e.stopPropagation();
				if(self.openMenu && !self.openMenu.is(':hidden')) {
					if(self.openMenu && !self.openMenu.is(':hidden')) {
						self.openMenu.hide();
						self.openMenu.parent('li').removeClass('selected');
						self.openMenu = false;
					} 
			 		
				} 
				if($(this).children('ul').is(':hidden')) {
					$(this).addClass('selected');
					$(this).children('ul').show();
					self.openMenu = $(this).children('ul');
				}
				else {
					$(this).removeClass('selected');
					$(this).children('ul').hide();
					self.openMenu = false;
				}
			}).mouseover(function() {
				if(self.openMenu) {
					self.openMenu.hide();
					self.openMenu.parent('li').removeClass('selected');
					$(this).addClass('selected').children('ul').show();
					self.openMenu = $(this).children('ul');
				}
			}).appendTo(menuElement);
		var menu = $('<ul/>').appendTo(button);
		if(menuInfo.class) {
			button.addClass(menuInfo.class);
		}
		this.menus[index] = menu;
		
		// Add all buttons to menu
		
		for(var i=0;i<menuInfo.length;i++) {
			var items = menuInfo[i];
			if(!items.clickAction) items.clickAction = false;
			if(!items.defaultState) items.defaultState = false;
			var html = items.display;
			if(items.buttons) {
				html = items.display + '<span class="torchd-ui-menu-arrow">&rarr;</span>';
			}
			if(!html) html = '';
			var li = $('<li/>').html(html).appendTo(menu);
			if(items.class) {
				li.addClass(items.class);
			}
			this.menus[index][items.display] = li;
			if(items.buttons) {
				var ul = $('<ul/>').appendTo(li);
				for(var n=0;n<items.buttons.length;n++) {
					
					var buttonInfo = items.buttons[n];
					
					if(buttonInfo.name == 'separator') {
						$('<li/>').addClass('torchd-ui-menu-separator').disableSelection().appendTo(ul);
					}
					else {
						var button = $('<li/>').html(buttonInfo.display).data('state', buttonInfo.defaultState).data('defaultHTML', buttonInfo.display).data('clickAction', buttonInfo.clickAction).click(function(e) {
							e.stopPropagation();
							if($(this).data('state') == 'enabled') {
								var action = $(this).data('clickAction');
								console.log(self.openMenu);
								self.openMenu.hide();
								
								self.openMenu.parent('li').removeClass('selected');
								self.openMenu = false;
								action();
							}
						}).disableSelection().addClass(buttonInfo.defaultState).appendTo(ul);
					}
				}
			}
			else {
				
				if(items.name == 'separator') {
					li.addClass('torchd-ui-menu-separator').disableSelection();
				}
				else {
					li.addClass(items.defaultState).data('state', items.defaultState).data('clickAction', items.clickAction).append('<span class="torchd-ui-menu-arrow"></span>').click(function(e) {

						e.stopPropagation();
						if($(this).data('state') == 'enabled') {
							console.log(self.openMenu);
							self.openMenu.parent('li').removeClass('selected');
							self.openMenu.css('display', 'none');
							self.openMenu = false;
							var action = $(this).data('clickAction');
							action();
						}
					}).disableSelection();
					
				}
				
			}
			
		}
	}
};

torchd.ui.menuBar.prototype.addMenuItem = function(buttonInfo) {
	var self = this;
	var parent = this.menus[buttonInfo.menu];
	if(buttonInfo.subMenu) {
		var parent = parent.children[buttonInfo.subMenu];
	}
	
	if(buttonInfo.position = 'before_separator') {
		var insertAfter = parent.children('.torchd-ui-menu-separator').prev('li');
	}
	else {
		var insertAfter = parent.children('li').last();
	}
	if(buttonInfo.name == 'separator') {
		$('<li/>').addClass('torchd-ui-menu-separator').disableSelection().insertAfter(insertAfter);
	}
	else {
		var button = $('<li/>').html(buttonInfo.display).data('state', buttonInfo.defaultState).data('defaultHTML', buttonInfo.display).data('clickAction', buttonInfo.clickAction).click(function(e) {
			e.stopPropagation();
			if($(this).data('state') == 'enabled') {
				var action = $(this).data('clickAction');
				self.openMenu.hide();
				
				self.openMenu.parent('li').removeClass('selected');
				self.openMenu = false;
				action();
			}
		}).disableSelection().addClass(buttonInfo.defaultState).insertAfter(insertAfter);
	}
	
};